<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('文章管理','/assets/module/dtree/dtree.css,/assets/module/dtree/font/dtreefont.css')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">板块：</label>
                        <div class="layui-input-inline mr0" style="width:200px;z-index: 199999;">
                            <ul id="catalogSelect" class="dtree" data-id="-1"></ul>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">类型：</label>
                        <div class="layui-input-inline mr0" style="width: 140px;">
                            <select id="postType">
                                <option value=""></option>
                                <option value="0">普通文章</option>
                                <option value="1">付费文章</option>
                                <option value="2">登陆可见</option>
                                <option value="3">回复可见</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                    <label class="layui-form-label w-auto">状态：</label>
                    <div class="layui-input-inline mr0" style="width: 100px;">
                        <select id="postState">
                            <option value=""></option>
                            <option value="top">置顶</option>
                            <option value="support">推荐</option>
                            <option value="state">状态</option>
                        </select>
                    </div>
                </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarOepration">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i
                class="layui-icon">&#xe654;</i>添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" ajax-event="batchDel" ajax-url="/admin/post/" ajax-id="postId">
            <i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="data-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/post/" value="{{d.postId}}">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="data-table-state">
    <input type="checkbox" lay-filter="data-table-state" ajax-url="/admin/postId/" value="{{d.postId}}" lay-skin="switch" lay-text="正常|禁用"
           {{d.state==0?'checked':''}}/>
</script>
<script type="text/html" id="data-table-top">
    <input type="checkbox" lay-filter="data-table-top" value="{{d.postId}}" lay-skin="switch" lay-text="置顶|取消"
           {{d.postTop==1?'checked':''}}/>
</script>
<script type="text/html" id="data-table-support">
    <input type="checkbox" lay-filter="data-table-support" value="{{d.postId}}" lay-skin="switch" lay-text="推荐|取消"
           {{d.postSupport==1?'checked':''}}/>
</script>

<!--类型-->
<script type="text/html" id="data-table-type">
    {{#  if(d.postType == 0){ }}
    <span class="layui-badge layui-bg-gray">普通文章</span>
    {{#  } else if(d.postType == 1){ }}
    <span class="layui-badge">付费文章</span>
    {{#  } else if(d.postType == 2){ }}
    <span class="layui-badge layui-bg-blue">登陆可见</span>
    {{#  } else if(d.postType == 3){ }}
    <span class="layui-badge layui-bg-green">回复可见</span>
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<script type="text/html" id="data-table-target">
    {{#  if(d.target == "_self"){ }}
    当前窗口打开
    {{#  } else if(d.target == "_blank"){ }}
    新窗口打开
    {{#  } else{ }}
    unkown
    {{#  }}}
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>

<script>
    layui.use(['form', 'table', 'util', 'admin','dtree'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var dtree = layui.dtree;

        var formUrl;  // 表单提交的action
        //渲染表格
        var tableIns = table.render({
            elem: '#data-table',
            url: '/admin/post/',
            page : true,
			loading : true,
            toolbar: '#toolbarOepration',
			height : 'full-115',
			limit : 10,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type: 'checkbox'},
                {field: 'postTitle',width:300, title: '文章名',templet: function (d) {
                        return '<a href="/post/'+ d.postId +'" target="_blank" style="text-decoration: underline">'+d.postTitle+'</a>';
                    }},
                {field: 'userName',width:100, title: '文章作者',templet: function (d) {
                        return d.author.userName;
                    }},
                {field: 'catalogName',width:140, sort: true, title: '文章板块',templet: function (d) {
                        return d.catalog.catalogName;
                    }},
                {field: 'postType',width:100,title: '文章类型',templet:'#data-table-type'},
                {field: 'commentCount',width:70,sort:true, title: '回复'},
                {field: 'state', width:100,title: '状态',templet: '#data-table-state'},
                {field: 'postTop', width:100,title: '置顶',templet: '#data-table-top'},
                {field: 'postSupport', width:100,title: '推荐',templet: '#data-table-support'},
                {
                    field: 'createTime',width:180, sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#data-table-bar', title: '编辑'}
            ]
            ]
        });
        //头工具栏事件
        table.on('toolbar(data-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    showEditModel();
                    break;
            };
        });
        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(data-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });
        var response = {
            statusName: "success", //返回标识（必填）
            statusCode: true, //返回码（必填）
            message: "msg", //返回信息（必填）
            rootName: "data", //根节点名称（必填）
            treeId: "id", //节点ID（必填）
            parentId: "parentId", //父节点ID（必填）
            title: "text", //节点名称（必填）
        };
        var CatalogTree = dtree.renderSelect({
            elem: "#catalogSelect",
            url: "/admin/catalog/treeSelect",
            method: 'GET',
            dataStyle: "layuiStyle",
            skin: "zdy",
            response:response,
            done: function(){
                //dtree.dataInit("selTree3", "001003");
                //dtree.setSelectValue("selTree3"); // 也可以在这里指定，第二个参数如果不填，则会自动读取
            }
        });
        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var keyword = $('#edtSearch').val();
            var param = {
                keyword:keyword,
                catalogId:dtree.getNowParam(CatalogTree).nodeId,
                postType:$('#postType').val(),
                postState:$('#postState').val()
            }
            tableIns.reload( {where: param},'data');
        });

        function doDelete(obj){
            top.layer.confirm('确定要删除吗？', function (i) {
                top.layer.close(i);
                B.del({
                    url:'/admin/post/'+obj.data.postId,
                    loading:true,
                    success:function(data){
                        if (data.success) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        // 显示编辑弹窗
        var showEditModel = function () {
           layer.confirm('暂时不支持后台添加文章？',{icon: 3, title: '提示信息',skin:'layui-layer-admin'}, function (index) {
               layer.close(index);
           });

        };
        // 修改状态
        form.on('switch(data-table-state)', function (obj) {
            var param = {
            postId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }
            updateState(obj,param);

        });
        // 修改置顶
        form.on('switch(data-table-top)', function (obj) {
            var param={
                postId:obj.elem.value,
                postTop:obj.elem.checked ? 1 : 0
            }
            updateState(obj,param);
        });
        // 修改推荐
        form.on('switch(data-table-support)', function (obj) {
            var param={
                postId:obj.elem.value,
                postSupport:obj.elem.checked ? 1 : 0
            }
            updateState(obj,param);
        });
        function updateState(obj,param){
            B.post({
                url: '/admin/post/updateState',
                loading: true,
                data: param,
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        tableIns.reload({},'data');// 重载表格
                    } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }

            });
        }

    });

</script>
</html>